package com.gillion.tms.service.impl;

import com.gfa4j.mybatis.dao.pojo.Page;
import com.gfa4j.mybatis.mapper.BaseMapper;
import com.gfa4j.mybatis.service.impl.BaseServiceImpl;

import com.gfa4j.util.StringUtils;
import com.gfa4j.utils.ResultUtils;
import com.gillion.business.mapper.StudentMapper;
import com.gillion.business.model.Student;

import com.gillion.business.model.StudentExample;
import com.gillion.tms.school.student.mapper.impl.StudentMapperImpl;
import com.gillion.tms.school.student.model.SelectStudentByNameParam;
import com.gillion.tms.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2016/10/24.
 */
@Service
public class StudentServiceImpl extends BaseServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentMapperImpl studentMapperImpl;

    @Override
    public BaseMapper getMapper() {
        return this.studentMapper;
    }

    @Override
    public Page findStudentRecords(Page page, String studentName) {
        SelectStudentByNameParam param=new SelectStudentByNameParam();
        param.setPage(page);
        if(StringUtils.isNoneBlank(studentName)){
            param.setStudentName(studentName);
        }
        return studentMapperImpl.selectStudentByName(param);
    }

    @Override
    public Map<String,Object> createStudent(Student student) {
        studentMapper.insertSelective(student);
        return ResultUtils.getSuccessResultData();
    }

    @Override
    public int deleteStudent(Long  studentId) {
        return studentMapper.deleteByPrimaryKey(studentId);
    }

    @Override
    public int updateStudent(Student student) {
        return studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    public Student selectStudentById(Long studentId) {
        return studentMapper.selectByPrimaryKey(studentId);
    }

    @Override
    public List<Student> getStudentByName(String  studentName,String studentNm) {
        StudentExample example=new StudentExample();
        if(StringUtils.isNoneBlank(studentName)) {
            example.createCriteria().andStudentNameLike("%" + studentName + "%");
        }
        if(StringUtils.isNoneBlank()) {
            example.createCriteria().andStudentNmLike("%" + studentNm + "%");
        }
        return studentMapper.selectByExample(example);
    }
}
